import java.util.EventListener;
import javax.swing.event.EventListenerList;


public class MessageService {
	private final EventListenerList listeners;
	
	public MessageService(){
		listeners = new EventListenerList();
	}
	
	public void addListener(MessageEventListener listener) {
		listeners.add(MessageEventListener.class, listener);
	}

	public void removeListener(MessageEventListener listener) {
		listeners.remove(MessageEventListener.class, listener);
	}

	public void post(String message) {
		post(message, new Object[0]);
	}

	public void post(String format, Object arg) {
		post(format, new Object[]{arg});
	}

	public void post(String format, Object arg1, Object arg2) {
		post(format, new Object[]{arg1, arg2});
	}

	public void post(String format, Object arg1, Object arg2, Object arg3) {
		post(format, new Object[]{arg1, arg2, arg3});
	}

	public void post(String format, Object[] args) {
		String message = String.format(format, args);
		MessageEvent event = new MessageEvent(message);
		for (EventListener listener: listeners.getListeners(MessageEventListener.class)) {
			((MessageEventListener)listener).post(event);
		}
	}

	public void clear() {
		post("");
	}


}
